package com.slz3.business.module.configManagement.conIndexNewGoods.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.configManagement.conIndexNewGoods.domain.ConIndexNewGoods;
import com.slz3.business.module.configManagement.conIndexNewGoods.domain.ConIndexNewGoodsFactory;
import com.slz3.business.module.configManagement.conIndexNewGoods.steward.ConIndexNewGoodsSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class ConIndexNewGoodsServiceImpl implements ConIndexNewGoodsService {

    @Override
    public ConIndexNewGoods save(ConIndexNewGoods conIndexNewGoods) {
        return Repositorys.conIndexNewGoods.save(ConIndexNewGoodsFactory.initBaseFields(conIndexNewGoods));
    }

    @Override
    public Page<ConIndexNewGoods> page(MultiValueMap<String, String> params) {
        Page<ConIndexNewGoods> conIndexNewGoodsPage = Repositorys.conIndexNewGoods.findAll(
                ConIndexNewGoodsSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return conIndexNewGoodsPage;
    }

    @Override
    public ConIndexNewGoods modify(ConIndexNewGoods conIndexNewGoodsNew) {
        return Repositorys.conIndexNewGoods.saveAndFlush(ConIndexNewGoodsFactory.initBaseFields(conIndexNewGoodsNew));
    }

    @Override
    public Optional<ConIndexNewGoods> single(UUID uuid) {
        return Repositorys.conIndexNewGoods.findById(uuid);
    }

    @Override
    public List<ConIndexNewGoods> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.conIndexNewGoods.findAllById(uuids);
    }

    @Override
    public List<ConIndexNewGoods> delete(List<ConIndexNewGoods> conIndexNewGoodsList) {
        conIndexNewGoodsList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            ConIndexNewGoodsFactory.initBaseFields(item);
        });
        return Services.conIndexNewGoodsTransactional.batchModify(conIndexNewGoodsList);
    }

}
